<?php
declare (strict_types=1);

namespace addons\Pay\driver;

use addons\Pay\exception\PayException;

class WeChat
{

    public static function create($config, $trade_type)
    {
        try {

            $wechat = new \WeChat\Pay($config->config['wechat']);
            $options = [
                'body'             => $config->subject,
                'out_trade_no'     => $config->out_trade_no,
                'total_fee'        => $config->total * 100,
                'notify_url'       => $config->notify_url,
                'trade_type'       => $trade_type,
                'spbill_create_ip' => request()->ip(),
                'openid'           => $config->openid,
            ];
            $result = $wechat->createOrder($options);
            if (empty($result['return_msg']) || $result['return_msg'] != 'OK') {
                throw new PayException('创建订单失败', -1);
            }
            switch ($trade_type) {
                case "JSAPI":
                    $options = $wechat->createParamsForJsApi($result['prepay_id']);
                    break;
                case "MWEB":
                    $options = $result['mweb_url'] . '&redirect_url=' . urlencode($config->redirect_url);
                    break;
                case "APP":
                    $options = $wechat->createParamsForApp($result['prepay_id']);
                    break;
                case "NATIVE":
                    $options = $wechat->createParamsForRuleQrc($result['prepay_id']);
                    break;
                default:
                    throw new PayException('trade_type为空或不支持其支付方式' - 1);
            }
            return $options;
        } catch (\Exception $e) {
            throw new PayException('创建订单失败：' . $e->getMessage(), -1);
        }
    }


    /**
     * 微信退款
     * 贡献者：张国磊
     * 日期：2023年5月30日11:07:06
     * @param $config
     * @return array
     * @throws PayException
     */
    public static function refund($config)
    {
        try {
            $wechat = new \WeChat\Pay($config->config['wechat']);
            $options = [
                'out_trade_no'  => $config->out_trade_no,
                'out_refund_no' => $config->out_refund_no,
                'total_fee'     => $config->total_fee * 100,
                'refund_fee'    => $config->refund_fee * 100,
            ];
            return $wechat->createRefund($options);
        } catch (\Exception $e) {
            throw new PayException('退款失败：' . $e->getMessage(), -1);
        }
    }

    /**
     * 微信退款查询
     * 贡献者：张国磊
     * 日期：2023年5月30日11:07:06
     * @param $config
     * @return array
     * @throws PayException
     */
    public static function queryRefund($config)
    {
        try {

            $wechat = new \WeChat\Pay($config->config['wechat']);
            $options = [
                'out_refund_no' => $config->out_refund_no
            ];
            $result = $wechat->queryRefund($options);

            if (empty($result['return_msg']) || $result['return_msg'] != 'OK') {
                throw new PayException('退款查询失败', -1);
            }
            return $result;
        } catch (\Exception $e) {
            throw new PayException('退款查询失败：' . $e->getMessage(), -1);
        }
    }

}